Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
Is there any differences between "this" and "super" when accessing parent class' property?

I always think "super" is the only way to access parent class' property, but i realized that "this" can also access parent class' property. Is there any differences betweet "super" and "this" in this situation?

class Human {
  constructor (name, age) {
    this.name = name;
    this.age = age;
  }
  mouse (sentense) {
    console.log(`${this.name}: "${sentense}".`);
  }
}

class Footballer extends Human {
  constructor(name, age, position, team) {
    super(name, age);
    this.team = team;
    this.position = position;
  }
  talk (sentense) {
    super.mouse(sentense); // This works.
    this.mouse(sentense); // This also works.
  }
}
about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

If you use this, the engine will first try to access the property on the instance itself. If it doesn't exist on the instance, it'll then go to the prototype (Footballer.prototype). If it doesn't exist there, it'll then go to Human.prototype to see if the property exists.

If you use super, it'll immediately go to the parent class, Human.prototype, to see if the property exists, and will go upward from there (rather than go through the instance and child prototype first).

This will be especially important to keep in mind if a method with the same name exists both on the parent and child, which isn't uncommon.

class Human {
  mouse () {
    console.log('parent class');
  }
}

class Footballer extends Human {
  mouse() {
    console.log('child class');
  }
  talk () {
    super.mouse();
    this.mouse();
  }
}

const f = new Footballer();
f.talk();

about 4 years ago · Santiago Gelvez Relatório

0

There is a big difference. In languages like Java and Javascript, with super you are accessing the parent's properties and methods.

With this you are accessing the child's properties and methods.

this.mouse() works because first the mouse property is accessed for the child (Footballer) and when not found the property is looked for in the parent (Human). This is how inheritance works. When accessing a property called x, Javascript will try to move upwards in the prototype chain until it finds a property named x.

If you define a property like kick only in Footballer, then super.kick() will not work but this.kick() will, demonstrating the difference.

class Human {
  constructor (name, age) {
    this.name = name;
    this.age = age;
  }
  mouse (sentense) {
    console.log(`${this.name}: "${sentense}".`);
  }
}

class Footballer extends Human {
  constructor(name, age, position, team) {
    super(name, age);
    this.team = team;
    this.position = position;
  }
  kick(){
  console.log("kicked");
  }
  play(){
    this.kick();
    super.kick();
  }
  talk (sentense) {
    super.mouse(sentense); // This works.
    this.mouse(sentense); // This also works.
  }
}

const messi = new Footballer();
messi.play();

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda